//
//  EmoticonViewModel.swift
//  表情键盘
//
//  Created by SteveLi on 2017/10/26.
//  Copyright © 2017年 李军桦. All rights reserved.
//

import Foundation
//MARK： --表情包的视图模型 --emoticon.plist
 class EmoticonManager {
    //单例
    static let sharedManager = EmoticonManager()
    //表情包模型
    lazy  var packages =  [EmoticonPackage]()
    //MARK: --构造函数
    init() {
        loadPlist()
    }
    
    //从emoticons.plist 加载表情数据
    func loadPlist(){
        //0.添加最近分组
        packages.append(EmoticonPackage(dict: ["group_name_cn":"最近A"]))
        //1.加载 emoticon.plist
        let path = Bundle.main.path(forResource: "emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        //2.加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: Any]
        //3.从字典中获得id的数组  -- valueForkey 直接获取字典数组中的 key 获取对应的数组
        let array = (dict["packages"] as! NSArray).value(forKey: "id")
        //4.遍历 id 数组,准备加载 info.plist
        for id in array as! [String] {
            loadInfoPlist(id: id)
        }
        
    }
    
    //加载每一个 id 目录下的 info.plist
    func loadInfoPlist(id:String){
        //1.建立路径
        let path = Bundle.main.path(forResource: "info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        //2.加载字典 --- 一个独立的表情包
        let dict = NSDictionary(contentsOfFile: path) as! [String:Any]
        //3.字典装模型追加到 packages 数组
        packages.append(EmoticonPackage(dict: dict))
    }
}
